//
//  POPMenu.m
//  彩宝宝, 弹出界面
//
//  Created by xh on 15/7/16.
//  Copyright (c) 2015年 com.caibb. All rights reserved.
//

#import "POPMenu.h"
#include "UIView+Extension.h"

@interface POPMenu()
/**
 *  将来用来显示具体内容的容器
 */
@property (nonatomic, weak) UIView *containerView;

@end

@implementation POPMenu

+ (instancetype)menu
{
    return [[self alloc] init];
}
- (UIView *)containerView
{
    if (!_containerView) {
        // 添加一个灰色图片控件
        UIView *containerView = [[UIView alloc] init];
        containerView.frame = [UIScreen mainScreen].bounds;
        [self addSubview:containerView];
        self.containerView = containerView;
    }
    return _containerView;
}

- (void)setContentController:(UIViewController *)contentController{
    _contentController = contentController;
    
    self.content = contentController.view;
}


- (void)setContent:(UIView *)content{
    _content = content;
    
    //CGFloat mainH = [UIScreen mainScreen].bounds.size.height;
    CGFloat mainW = [UIScreen mainScreen].bounds.size.width;
    
    // 调整内容的位置
    content.x = mainW * 0.05;
    //content.y = 70;
    content.y = (LSScreenHeight-content.height)/2.0;
    //    // 设置灰色的高度
    //    self.containerView.height = CGRectGetMaxY(content.frame) + 11;
    //    // 设置灰色的宽度
    //    self.containerView.width = CGRectGetMaxX(content.frame) + 10;
    
    // 添加内容到灰色图片中
    [self.containerView addSubview:content];
}



- (void)setContentController1:(UIViewController *)contentController1
{
    _contentController1 = contentController1;
    
    self.content1 = contentController1.view;
}

- (void)setContent1:(UIView *)content1
{
    _content1 = content1;
    
    CGFloat mainH = [UIScreen mainScreen].bounds.size.height;
    //CGFloat mainW = [UIScreen mainScreen].bounds.size.width;
    
    // 调整内容的位置
    content1.x = 0;
    content1.y = mainH - 90 - content1.height;

    //    // 设置灰色的高度
    //    self.containerView.height = CGRectGetMaxY(content.frame) + 11;
    //    // 设置灰色的宽度
    //    self.containerView.width = CGRectGetMaxX(content.frame) + 10;
    
    // 添加内容到灰色图片中
    [self.containerView addSubview:content1];
}



-(void)setContentHead:(UIView *)contentHead
{
    _content = contentHead;
    
    _content1 = contentHead;
    
    contentHead.x = 0;
    if (self.contentHeadInt==11) {
        
        contentHead.y = 115;
        
    }else{
        
        contentHead.y = 65;
    }
    
    
    if (self.contentHeadInt==10) {
        
       contentHead.backgroundColor=[UIColor whiteColor];
        
    }else{
        
    contentHead.backgroundColor=Color(235.0, 235.0, 235.0);
        
    }
    [self.containerView addSubview:contentHead];

}


/**
 *  显示
 */
- (void)showFrom:(UIView *)from
{
    
//    if (self.contentController != nil) {
//        
//        CGFloat ViewY = from.height - self.contentController.view.height;
//        
//        [UIView animateWithDuration:3.0 animations:^{
//            self.frame = CGRectMake(0, ViewY, from.width, self.contentController.view.height);
//        }];
//        
//    }
//
   
    
    // 1.获得最上面的窗口
    UIWindow *window;// = [[UIApplication sharedApplication].windows lastObject];
    if ( @available(iOS 11.0, *)) {
        window = [UIApplication sharedApplication].keyWindow;
    }else{
        window = [[UIApplication sharedApplication].windows lastObject];
    }
    // 2.添加自己到窗口上
    [window addSubview:self];
    
    
    
//    MyLog(@"%@",NSStringFromCGRect(UIWindowLevelStatusBar)); //结果都是{{0, 0}, {320, 460}}。
    
//    当viewController.wantsFullScreenLayout=YES;时，view左顶点和状态栏相同，即view和statuBar都开始于window左顶点上；
//    MyLog(@"%@",UIWindowLevelStatusBar)
    // 3.设置尺寸
    self.frame =window.bounds;


    
    // 4.调整灰色图片的位置
    // 默认情况下，frame是以父控件左上角为坐标原点
    // 转换坐标系
//    CGRect newFrame = [from convertRect:from.bounds toView:window];
//  //  CGRect newFrame = [from.superview convertRect:from.frame toView:window];
//    self.containerView.centerX = CGRectGetMidX(newFrame);
//    self.containerView.y = CGRectGetMaxY(newFrame);
    // 通知外界，自己显示了
    if ([self.delegate respondsToSelector:@selector(dropdownMenuDidShow:)]) {
        [self.delegate dropdownMenuDidShow:self];
    }
}



/**
 *  销毁
 */
- (void)dismiss
{
    [self removeFromSuperview];
    
    
    // 通知外界，自己被销毁了
    if ([self.delegate respondsToSelector:@selector(dropdownMenuDidDismiss:)])
    {
        [self.delegate dropdownMenuDidDismiss:self];
    }
    
}

-(void)dealloc{
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.isClick==NO) {
        [self dismiss];
    }
    
  
}
@end
